﻿import ajaxOptions = require("AjaxCallsOptions");
import config = require("Configuration");
import dataAccess = require("Data-access");
import application = require("WindowExtensions");
import models = require("Models");

export class BookTickets extends kendo.data.ObservableObject {

    selectedTheater: models.Theater;
    selectedMovie: models.MovieForTheater;
    noAdultTickets: number = 0;
    noChildTickets: number = 0;
    private _instance;

    constructor() {
        super();
        super.init(this);
        this._instance = this;
    }

    incrementChildTicket() {
        this.set("noChildTickets", parseInt(this.get("noChildTickets")) + 1);
    }

    decrementChildTicket() {
        if (this.noChildTickets > 0) {
            this.set("noChildTickets", this.noChildTickets - 1);
        }
    }

    noChildTotalAmount() {
        return this.noChildTickets * 8;
    }

    incrementAdultTicket() {
        this.set("noAdultTickets", parseInt(this.get("noAdultTickets")) + 1);
    }

    decrementAdultTicket() {
        if (this.noAdultTickets > 0) {
            this.set("noAdultTickets", this.noAdultTickets - 1);
        }
    }

    noAdultTotalAmount() {
        return this.noAdultTickets * 10;
    }

    totalAmount() {
        return this.noChildTotalAmount() + this.noAdultTotalAmount();
    }

    show(e) {
        var movie = new models.MovieForTheater();
        movie.movieId = e.view.params.movieId;
        movie.movieName = decodeURIComponent(e.view.params.movieName);
        movie.rating = e.view.params.rating;
        this.set("selectedMovie", movie);

        var theater = new models.Theater();
        theater.theaterId = e.view.params.theaterId;
        theater.theaterName = decodeURIComponent(e.view.params.theaterName);
        theater.address = decodeURIComponent(e.view.params.address);
        theater.time = decodeURIComponent(e.view.params.time);
        theater.date = decodeURIComponent(e.view.params.date);
        this.set("selectedTheater", theater);
    }

    purchaseTickets() {
        if (this.noAdultTickets == 0 &&
            this.noChildTickets == 0) {
            alert('Atleast one ticket needs to be purchased.');
            return;
        }

        var ticketOptions = new ajaxOptions.AjaxOptions();
        ticketOptions.url = config.Configuration.purchaseTicketsUrl;
        var data = new models.TicketPurchaseModel();
        data.movieId = this.selectedMovie.movieId;
        data.theaterId = this.selectedTheater.theaterId;
        data.noOfAdultTickets = this.noAdultTickets;
        data.noOfChildTickets = this.noChildTickets;
        data.showDate = this.selectedTheater.date;
        data.showTime = this.selectedTheater.time;
        data.totalAmount = this.totalAmount();
        ticketOptions.data = data;
        ticketOptions.dataType = "JSON";
        ticketOptions.requestType = "POST";

        dataAccess.callService(ticketOptions, this._instance, this.fnPurchaseCallBack);
    }

    closeModalPopup() {
        //close the confirmation modal popup
        $("#mt-modal-complete").data("kendoMobileModalView").close();
        //navigate to the home page
        var app = application.getApplication();
        app.navigate('#mt-home-main-view');
    }

    fnPurchaseCallBack(model: kendo.data.ObservableObject, result: ajaxOptions.AjaxOptionsCallbackInput) {
        if (result.success === true) {
            //display a message to the user
            //using modal view
            var intializedModalView =
                $("#mt-modal-complete").data("kendoMobileModalView");
            //if the modal view is already initialized,
            //destroy it
            if (intializedModalView) {
                intializedModalView.destroy();
            }

            $('#mt-modal-complete').kendoMobileModalView({
                height: 130,
                width: 200
            }).data("kendoMobileModalView").open();
        } else {
            alert('purchase failed. please try again.');
        }
    }
}

var appInit = application.getAppInitProperty();
var viewData = application.getViewDataProperty();
appInit.ticketsInit = () => {
    var viewModel = new BookTickets();
    viewData.TicketsViewModel = viewModel;
    kendo.bind($("#mt-book-tickets-view"), viewModel);
}